## This code include scripts for Appendix Tables
# Table A1

rm(list = ls())
library(readr)
library(ggplot2)
library(stargazer)
library(lme4)
library(dplyr)
library(haven)
library(texreg)
library(tidyr)

# variables:
#"onset_ko_flag":Binary flag indicating group-level conflict onset / ko option
# "onset_ko_terr_flag": binary flag indicating group-level territorial conflict onset  / ko option
# "onset_ko_gov_flag: binary flag indicating group-level governmental conflict onset  / ko option
# onset_do_flag": binary flag indicating group-level conflict onset: / do option
# "onset_do_terr_flag": binary flag indicating group-level territorial conflict onset  / do option  
# "onset_do_gov_flag": binary flag indicating group-level governmental conflict onset  / do option  


load("Final_JPR/data/paperdata.RData")
data <- paperdata %>%
       filter(tek_count>0)

source("Final_JPR/Code/coefplot.R")
source("Final_JPR/Code/marginal_effects.R")
source("Final_JPR/Code/setup.R")


#######################################  Table A1 （relative poverty and relative wealth) ######################################################################

## robustness checks: relative terms

## dependent variable
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_onset_relative <- list()
for (i in 1:length(ivs)){
        model_onset_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                         data = data, family = binomial(link = "logit"))
}


## dependent variable
dv <- 'incidence_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_incidence_relative <- list()
for (i in 1:length(ivs)){
        model_incidence_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                             data = data, family = binomial(link = "logit"))
}




######## Appendix Table A1

modSD = cluster_se(ModelResults = c(model_onset_relative, model_incidence_relative), data = data, 
                   clusterid = "gwgroupid") 

texreg(c(model_onset_relative, model_incidence_relative ), file = "Final_JPR/tables/appendix_tables/Table_A1.tex",
       stars = c(0.001, 0.01, 0.05),
       override.se =  modSD,
       caption = "Logistic regression results of upward and downward comparisons for ethnic conflict (1992-2020)",
       caption.above = TRUE,
       label = "tab:tab5",
       scalebox = 0.7,
       use.packages = FALSE,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Prevalence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                              "best_high_tlineq" = "Relative wealth",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_low_tlineq" ="Relative poverty",
                              "worst_high_tlineq" = "Relative wealth",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_low_tlineq" = "Relative poverty",
                              "median_high_tlineq" =  "Relative wealth",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_low_tlineq" ="Relative poverty",
                              "nearst_high_tlineq" =  "Relative wealth",
                              "teknearst_status_excl" ="TEK status excluded",
                              "peaceyears" = "Peace year",
                              "peaceyears_sq" =  "Peace year $^2$",
                              "peaceyears_cub" = "Peace year $^2$",
                              "(Intercept)" = "Intercept"),
       digits = 3)


